package utility;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * Questa classe effettua il parsing dei file di testo in input per la creazione del circuito.
 * @author Giovanni Richini
 * @author Michael Maghella
 * @version 1.0
 *
 */
public class Parser {
	
	/**
	 * Istanza di parsedGraph
	 * @uml.property  name="parsedGraph"
	 */
	ParsedGraph parsedGraph;
	
	/**
	 * Costruttore della classe.
	 */
	public Parser(){
		parsedGraph = new ParsedGraph();
	}
	
	/**
	 * Metodo per il parsing del circuito.
	 * @param input_path
	 * @return parsedGraph
	 */
    public ParsedGraph parseText(String input_path) throws IOException {
		
        Path path = Paths.get(input_path);
        Scanner scanner = new Scanner(path);
         
        //read file line by line
        scanner.useDelimiter(System.getProperty("line.separator"));
        while(scanner.hasNext()){
        	String var =scanner.next().toString();
        		parseLine(var);

        }
        scanner.close();
        return parsedGraph;
    }
    

		
	/**
	 * Metodo per l'estrazione del nome del circuito dal path.
	 * @param input_path
	 */
	public String extractName(String input_path){
		int lastSlash=input_path.indexOf("\\")+1;
		int point=input_path.indexOf(".");
		String name=input_path.substring(lastSlash, point);
		return name;
	}
	

		
	/**
	 * Metodo per il parsing del file relativo ai valori iniziali dell'input, di supporto a: parseInputVector.
	 * @param var
	 */
	public void parseLine(String var){
		
		Pattern pattern = Pattern.compile("OUTPUT\\((\\S*)\\)\\s*=(\\S*),\\{(\\S*)\\}\\s*\\[(\\S*)\\]");
		Matcher matcher = pattern.matcher(var); 
    	//System.out.println(var);
        if (matcher.find())
        {
        	////System.out.println("\n"+var);
        	//System.out.println("1)"+matcher.group(1));
        	//parsedGraph.addOutput(matcher.group(1)); in caso servano gli output
        	//System.out.println("2)"+matcher.group(2));
        	//System.out.println("3)"+matcher.group(3));
        	parseVertex(matcher.group(3),matcher.group(2),matcher.group(1));
        	//System.out.println("4)"+matcher.group(4));
        	parseEdges(matcher.group(4));
        	//parsedGraph.stampa();
        }
        
	}
	
	private void parseVertex(String archi, String value, String root){
    	ArrayList<String> vertex= new ArrayList<String>();
    	vertex.addAll(Arrays.asList(archi.split("\\s*,\\s*")));

    	for(int i=0; i<vertex.size(); i++){
    			this.parsedGraph.addVertex(vertex.get(i));
            }
    	parsedGraph.addCono(vertex, value, root);
    	}
	
	private void parseEdges(String archi){
    	ArrayList<String> edges= new ArrayList<String>();
    	edges.addAll(Arrays.asList(archi.split("\\s*,\\s*")));

    	for(int i=0; i<edges.size(); i++){
    		Pattern pattern = Pattern.compile("(\\S*)-(\\S*)");
    		Matcher matcher = pattern.matcher(edges.get(i));
    		if (matcher.find())
    			this.parsedGraph.addArc(matcher.group(1), matcher.group(2));
            }    		    		
    	}
	
public ArrayList<Integer> parseDiagnosi(String line){
	ArrayList<Integer> diagnosiInt = new ArrayList<Integer>();
	
	ArrayList<String> diagnosi= new ArrayList<String>();
	diagnosi.addAll(Arrays.asList(line.split("\\s*,\\s*")));
    for(int i=0; i<diagnosi.size(); i++){
		Pattern pattern = Pattern.compile("h([0-9])");
		Matcher matcher = pattern.matcher(diagnosi.get(i));
		if (matcher.find()){
			
			diagnosiInt.add((Integer.parseInt(matcher.group(1))));
        }   
    }
       
    
    return diagnosiInt;
	}

/**
 * Metodo per il parsing del circuito.
 * @param input_path
 * @return parsedGraph
 */
public ArrayList<ArrayList<Integer>> parseStaccato(String input_path) throws IOException {
	
	ArrayList<ArrayList<Integer>> parsed = new ArrayList<ArrayList<Integer>>();
    Path path = Paths.get(input_path);
    Scanner scanner = new Scanner(path);
     
    //read file line by line
    scanner.useDelimiter(System.getProperty("line.separator"));
    while(scanner.hasNext()){
    	String var =scanner.next().toString();
    		parsed.add(this.parseDiagnosi(var));

    }
    scanner.close();
    //System.out.println("Quindi alla fine");
    String str="";
    for(int i=0; i< parsed.size(); i++){
    	//str="solution: ";
    	for(int j=0; j< parsed.get(i).size(); j++){
        	str+=parsed.get(i).get(j)+" ";
        }
    	//System.out.println(str);
    }
    return parsed;
}
    
	
}